package com.flyiu.data.consistency.aspect;

import java.lang.reflect.Method;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

import com.flyiu.data.consistency.annotations.DistributedTransactionPartner;
import com.flyiu.data.consistency.service.PartnerService;

@Aspect
@Component
@Order
public class DistributedTransactionPartnerAspect {
	@Autowired
	private PartnerService service;
	
    /**
     * 二阶段
     *
     * @param joinPoint 切面链接点
     * @return  结果对象
     * @throws Throwable    异常信息
     */
    @Around("com.flyiu.data.consistency.aspect.DistributedTransactionPointCut.distributedTransactionPartner()")
    public Object invokeAction(ProceedingJoinPoint joinPoint) throws Throwable {
        Signature signature = joinPoint.getSignature();
        MethodSignature methodSignature = (MethodSignature) signature;
        Method method = methodSignature.getMethod();
        DistributedTransactionPartner distributedTransaction =  method.getAnnotation(DistributedTransactionPartner.class);
        service.start(distributedTransaction.name(),joinPoint.getArgs());
        return joinPoint.proceed();
    }
    
    
}
